﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraEditors;

using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.ADF.BaseClasses;


namespace SZ
{
    class SelectClass : BaseTool
    {
        private AxMapControl mapControl = null;
        public SelectClass(AxMapControl axMapControl)
        {
            mapControl = axMapControl;
        }

        public override void OnCreate(object hook)
        {

        }

        public override void OnClick()
        {


        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            IPoint pPoint = mapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);

            //取消高亮显示的要素
            IMap pMap = mapControl.Map;
            pMap.ClearSelection();

            double realWorldDisplayExtent;
            int pixelExtent;
            double sizeOfOnePixel;

            pixelExtent = mapControl.ActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().right - mapControl.ActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().left;
            realWorldDisplayExtent = mapControl.ActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
            sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;

            double bufferLength = sizeOfOnePixel * 2;

            ITopologicalOperator pTopOperator = pPoint as ITopologicalOperator;
            IGeometry pGeometry = pTopOperator.Buffer(bufferLength);

            for (int i = 0; i < pMap.LayerCount; i++)
            {
                ILayer pLayer = pMap.get_Layer(i);
                if (pLayer.Visible == true)
                {
                    IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                    IFeatureClass pFeaureClass = pFeatureLayer.FeatureClass;
                    ISpatialFilter pSpatialFilter = new SpatialFilterClass();
                    pSpatialFilter.Geometry = pGeometry;
                    pSpatialFilter.GeometryField = pFeaureClass.ShapeFieldName;

                    //pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                    //IFeatureCursor pFeatureCursor = pFeaureClass.Search(pSpatialFilter, false);

                    //IFeature pFeauture = pFeatureCursor.NextFeature();

                    IFeatureSelection pFeatureSelection = pLayer as IFeatureSelection;
                    pFeatureSelection.SelectFeatures(pSpatialFilter as IQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
                    mapControl.ActiveView.Refresh();


                }
            }
        }

    }
}
